package com.gxuwz.attend.dao.instructor;

import com.gxuwz.attend.entity.Student;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

public class StudentDao {

    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public List<Student> getAllStudents(int size, int offset) {
        String sql = "SELECT * FROM sys_student LIMIT ? OFFSET ?";
        return jdbcTemplate.query(sql, new Object[]{size, offset}, (rs, rowNum) -> {
            Student student = new Student();
            student.setStuID(rs.getString("stuID"));
            student.setPassword(rs.getString("password"));
            student.setClassID(rs.getString("classID"));
            student.setStuName(rs.getString("stuName"));
            student.setSex(rs.getString("sex"));
            student.setAddress(rs.getString("address"));
            student.setStuTel(rs.getString("stuTel"));
            student.setContact(rs.getString("contact"));
            student.setContactTel(rs.getString("contactTel"));
            return student;
        });
    }

    public void addStudent(Student student) {
        String sql = "INSERT INTO sys_student ( stuID, password, classID, stuName, sex, address, stuTel, contact, contactTel) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        jdbcTemplate.update(sql,  student.getStuID(), student.getPassword(), student.getClassID(),
                student.getStuName(), student.getSex(), student.getAddress(), student.getStuTel(),
                student.getContact(), student.getContactTel());
    }

    public boolean deleteStudent(String stuID) {
        String sql = "DELETE FROM sys_student WHERE stuID=?";
        int rs = jdbcTemplate.update(sql, stuID);
        return rs > 0; // 返回是否删除成功
    }

    public boolean updateStudent(Student student) {
        String sql = "UPDATE sys_student SET stuID=?, password=?, classID=?, stuName=?, sex=?, address=?, stuTel=?, contact=?, contactTel=? WHERE stuID=?";
        int rs = jdbcTemplate.update(sql,student.getStuID(), student.getPassword(), student.getClassID(), student.getStuName(),
                student.getSex(), student.getAddress(), student.getStuTel(), student.getContact(),
                student.getContactTel(), student.getId());
        return rs > 0; // 返回是否更新成功
    }

    public List<Student> findStudents(String stuID, String stuName, int size, int offset) {
        StringBuilder sql = new StringBuilder("SELECT * FROM sys_student WHERE 1=1");
        List<Object> params = new ArrayList<>();

        if (stuID != null && !stuID.isEmpty()) {
            sql.append(" AND stuID LIKE ?");
            params.add("%" + stuID + "%");
        }

        if (stuName != null && !stuName.isEmpty()) {
            sql.append(" AND stuName LIKE ?");
            params.add("%" + stuName + "%");
        }

        sql.append(" LIMIT ? OFFSET ?");
        params.add(size);
        params.add(offset);

        return jdbcTemplate.query(sql.toString(), params.toArray(), (rs, rowNum) -> {
            Student student = new Student();
            student.setStuID(rs.getString("stuID"));
            student.setPassword(rs.getString("password"));
            student.setClassID(rs.getString("classID"));
            student.setStuName(rs.getString("stuName"));
            student.setSex(rs.getString("sex"));
            student.setAddress(rs.getString("address"));
            student.setStuTel(rs.getString("stuTel"));
            student.setContact(rs.getString("contact"));
            student.setContactTel(rs.getString("contactTel"));
            return student;
        });
    }
}
